package com.xqboss.web.controller.system;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.consumer.ConsumerFamiliarProblem;
import com.xqboss.apps.domain.sys.SysFamiliarProblem;
import com.xqboss.apps.dto.system.SysFamiliarProblemDto;
import com.xqboss.apps.service.consumer.ConsumerFamiliarProblemService;
import com.xqboss.apps.service.sys.SysFamiliarProblemService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.LocalDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 常见问题接口
 *
 * @author wangcong
 * @date 2023-09-01
 */
@Api(tags = {"常见问题"})
@RestController
@RequestMapping("/web/familiar/problems")
public class WebSysFamiliarProblemController extends BaseController {
    @Autowired
    private SysFamiliarProblemService sysFamiliarProblemService;
    @Autowired
    private ConsumerFamiliarProblemService consumerFamiliarProblemService;

    @PreAuthorize("@ss.hasPermi('web:famliar:problems:add')")
    @ApiOperation("新增常见问题")
    @PostMapping("add")
    @Log(title = "常见问题", businessType = BusinessType.INSERT)
    public R add(@RequestBody SysFamiliarProblem sysFamiliarProblem) {
        sysFamiliarProblemService.save(sysFamiliarProblem);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:famliar:problems:findById')")
    @ApiOperation("根据主键查询")
    @GetMapping("findById/{id}")
    public R<SysFamiliarProblem> findByKey(@PathVariable("id") Long id) {
        return R.ok(sysFamiliarProblemService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('web:famliar:problems:update')")
    @ApiOperation("修改常见问题")
    @PutMapping("update")
    @Log(title = "常见问题", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysFamiliarProblem dto) {
        if (StrUtil.isNotBlank(dto.getTitle())
                || StrUtil.isNotBlank(dto.getSolution())
                || StrUtil.isNotBlank(dto.getOperateImg())) {
            dto.setUnsolved(0);
            dto.setResolved(0);
            //刷新之前的已解决未解决
            LambdaQueryWrapper<ConsumerFamiliarProblem> wp = new LambdaQueryWrapper<>();
            wp.eq(ConsumerFamiliarProblem::getProblemId, dto.getId());
            consumerFamiliarProblemService.remove(wp);
        }
        dto.setUpdateTime(LocalDateUtils.getCurrentLocalDateTime());
        sysFamiliarProblemService.updateById(dto);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:famliar:problems:list')")
    @ApiOperation("查询列表")
    @GetMapping("list")
    public R<List<SysFamiliarProblem>> findBy(SysFamiliarProblemDto dto) {
        dto.startPage();
        LambdaQueryWrapper<SysFamiliarProblem> wq = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(dto.getKeyword())) {
            wq.like(SysFamiliarProblem::getTitle, dto.getKeyword());
            wq.or(wrapper -> wrapper.like(SysFamiliarProblem::getSolution, dto.getKeyword()));
        }
        if (ObjUtil.isNotNull(dto.getSaleable())) {
            wq.eq(SysFamiliarProblem::getSaleable, dto.getSaleable());
        }
        wq.orderByDesc(SysFamiliarProblem::getCreateTime);

        List<SysFamiliarProblem> list = sysFamiliarProblemService.list(wq);
        return R.ok(list);
    }
}
